﻿using System;
using System.Collections.Generic;
using System.Linq;
using Hongsh.Pay.Client.Proxy.Http;
using MapleLeaf.Pay;
using MapleLeaf.Pay.Pay;
using MapleLeaf.Pay.Trade;

namespace Hongsh.Pay.Client.Proxy.Handlers {
    // Token: 0x0200005A RID: 90
    public static class LEPayProxy {
        // Token: 0x06000301 RID: 769 RVA: 0x00003E34 File Offset: 0x00002034
        public static void AddLEPayProxyHandler(this HttpServer svr) {
            svr.RouterList.Add("POST/cgi-bin/lepos_pay_gateway.cgi", new HttpContextActionDelegate(Process));
        }

        // Token: 0x06000302 RID: 770 RVA: 0x0000E2D4 File Offset: 0x0000C4D4
        private static void Process(HttpContext ctx) {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            dictionary["resp_code"] = "0";
            dictionary["resp_msg"] = "";
            dictionary["result_code"] = "0";
            Dictionary<string, object> dictionary2 = dictionary;
            try {
                string a = ctx.Request.Form["service"];
                string text = ctx.Request.Form["third_order_id"];
                dictionary2["merchant_id"] = ctx.Request.Form["merchant_id"];
                dictionary2["third_order_id"] = dictionary2["leshua_order_id"] = text;
                HongshPayTrade hongshPayTrade = null;
                if (a == "query_status" || a == "upload_authcode") {
                    if (a == "upload_authcode") {
                        string text2;
                        TradeAuthPaymentRequest tradeAuthPaymentRequest = new TradeAuthPaymentRequest {
                            ClientTradeId = text,
                            Amount = Convert.ToInt32(ctx.Request.Form["amount"]),
                            Authcode = ctx.Request.Form["auth_code"].ToString(),
                            Subject = ctx.Request.Form.TryGetValue("body", out text2) ? text2.ToString() : text
                        };
                        try {
                            string json;
                            if (ctx.Request.Form.TryGetValue("goods_detail", out json)) {
                                LEPayGoodsDetail lepayGoodsDetail = json.JsonDecode<LEPayGoodsDetail>();
                                tradeAuthPaymentRequest.GoodsList = (from d in lepayGoodsDetail.goods_detail
                                                                     select new TradeGoods {
                                                                         Id = d.goods_id,
                                                                         Name = d.goods_name,
                                                                         Price = d.price,
                                                                         Quantity = d.quantity
                                                                     }).ToArray<TradeGoods>();
                            }
                        }
                        catch {
                        }
                        hongshPayTrade = ctx.Execute(tradeAuthPaymentRequest);
                    }
                    else {
                        hongshPayTrade = ctx.Execute(new TradePaymentQueryRequest {
                            ClientTradeId = text
                        });
                    }
                    switch (hongshPayTrade.Payway) {
                        case TradePayway.微信:
                            dictionary2["pay_way"] = "WXZF";
                            break;
                        case TradePayway.支付宝:
                            dictionary2["pay_way"] = "ZFBZF";
                            break;
                        case TradePayway.云闪付:
                            dictionary2["pay_way"] = "UPSMZF";
                            break;
                    }
                    TradeState state = hongshPayTrade.State;
                    TradeState tradeState = state;
                    if (tradeState != TradeState.成功) {
                        if (tradeState - TradeState.关闭 > 1) {
                            dictionary2["status"] = "0";
                        }
                        else {
                            dictionary2["status"] = "8";
                        }
                    }
                    else {
                        dictionary2["status"] = "2";
                    }
                }
                else if (a == "refund" || a == "query_refund") {
                    if (a == "refund") {
                        hongshPayTrade = ctx.Execute(new TradeRefundRequest {
                            ClientTradeId = text,
                            RefundAmount = -1
                        });
                    }
                    else {
                        hongshPayTrade = ctx.Execute(new TradeRefundQueryRequest {
                            ClientTradeId = text
                        });
                    }
                    dictionary2["status"] = "11";
                }
                dictionary2["error_msg"] = dictionary2["resp_msg"] = hongshPayTrade.StateDescript;
                dictionary2["amount"] = (hongshPayTrade.Amount * 100).ToString("0.00");
            }
            catch (Exception ex) {
                dictionary2["result_code"] = "-4027";
                dictionary2["error_code"] = dictionary2["resp_code"] = "-20005";
                dictionary2["error_msg"] = dictionary2["resp_msg"] = ex.Message;
            }
            ctx.Result = new GClass0(dictionary2, "leshua");
        }

        // Token: 0x0200005B RID: 91
        private class GoodsDetail {
            // Token: 0x17000150 RID: 336
            // (get) Token: 0x06000303 RID: 771 RVA: 0x00003E52 File Offset: 0x00002052
            // (set) Token: 0x06000304 RID: 772 RVA: 0x00003E5A File Offset: 0x0000205A
            public string goods_id { get; set; }

            // Token: 0x17000151 RID: 337
            // (get) Token: 0x06000305 RID: 773 RVA: 0x00003E63 File Offset: 0x00002063
            // (set) Token: 0x06000306 RID: 774 RVA: 0x00003E6B File Offset: 0x0000206B
            public string goods_name { get; set; }

            // Token: 0x17000152 RID: 338
            // (get) Token: 0x06000307 RID: 775 RVA: 0x00003E74 File Offset: 0x00002074
            // (set) Token: 0x06000308 RID: 776 RVA: 0x00003E7C File Offset: 0x0000207C
            public int price { get; set; }

            // Token: 0x17000153 RID: 339
            // (get) Token: 0x06000309 RID: 777 RVA: 0x00003E85 File Offset: 0x00002085
            // (set) Token: 0x0600030A RID: 778 RVA: 0x00003E8D File Offset: 0x0000208D
            public int quantity { get; set; }

            // Token: 0x0600030B RID: 779 RVA: 0x0000286D File Offset: 0x00000A6D
            public GoodsDetail() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x0200005C RID: 92
        private class LEPayGoodsDetail {
            // Token: 0x17000154 RID: 340
            // (get) Token: 0x0600030C RID: 780 RVA: 0x00003E96 File Offset: 0x00002096
            // (set) Token: 0x0600030D RID: 781 RVA: 0x00003E9E File Offset: 0x0000209E
            public GoodsDetail[] goods_detail { get; set; }

            // Token: 0x0600030E RID: 782 RVA: 0x0000286D File Offset: 0x00000A6D
            public LEPayGoodsDetail() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }
    }
}
